<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%-- 页头 --%>
    <%@include file="../common/head.jsp" %>
</head>
<body>
<div class="wrapper">

    <%-- 导航 --%>
    <%@include file="manage-header.jsp" %>

    <%-- 侧边栏 --%>
    <%@include file="manage-sidebar.jsp" %>

    <div class="main-panel">
        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="card-title">更新分类</div>
                            </div>
                            <div class="card-body">
                                <div class="row justify-content-md-center">
                                    <div class="col-md-8">
                                        <div class="card">
                                            <div class="card-body">
                                                <form id="updateAcForm">
                                                    <div class="form-group">
                                                        <label>分类编号</label>
                                                        <input type="text" name="acId" value="${articleCategory.acId}"
                                                               disabled class="form-control input-square acId">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>分类标题</label>
                                                        <input type="text" name="acTitle"
                                                               value="${articleCategory.acTitle}"
                                                               class="form-control input-square acTitle">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>分类介绍</label>
                                                        <input type="text" name="acIntroduce"
                                                               value="${articleCategory.acIntroduce}"
                                                               class="form-control input-square acIntroduce">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>分类图标</label>
                                                        <input type="text" name="acIcon"
                                                               value="${articleCategory.acIcon}"
                                                               class="form-control input-square acIcon">
                                                    </div>
                                                    <div class="form-group text-center">
                                                        <label class="form-radio-label">
                                                            <input class="form-radio-input acState" type="radio"
                                                                   name="acState" value="0">
                                                            <span class="form-radio-sign">启用分类</span>
                                                        </label>
                                                        <label class="form-radio-label">
                                                            <input class="form-radio-input acState" type="radio"
                                                                   name="acState" value="1">
                                                            <span class="form-radio-sign">禁用分类</span>
                                                        </label>
                                                    </div>
                                                </form>
                                            </div>
                                            <div class="card-action text-center">
                                                <button type="button" class="btn btn-info btn-success" id="updateAcBtn">
                                                    更新分类
                                                </button>
                                                <button type="button" class="btn btn-info btn-danger"
                                                        onclick="window.location.href='/manage/article/category/list'">
                                                    返回管理
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <%-- 页脚 --%>
        <%@include file="../common/footer.jsp" %>

    </div>
</body>

<%-- js --%>
<%@include file="../common/script.jsp" %>

<script>
    $(function () {
        // 选中分类状态
        var acState = parseInt('${articleCategory.acState}');
        switch (acState) {
            case 0: $(".acState").eq(0).attr('checked', true); break;
            case 1: $(".acState").eq(1).attr('checked', true); break;
            default: alert("设置选中状态bug");
        }

        // 更新分类
        $("#updateAcBtn").on("click", function () {
            var acId = $(".acId").val();
            var acTitle = $(".acTitle").val();
            var acIntroduce = $(".acIntroduce").val();
            var acIcon = $(".acIcon").val();
            var acState = $('.acState:checked').val();
            if (acId != null && acId.length != 0) {
                if (acTitle != null && acTitle.length != 0) {
                    if (acIntroduce != null && acIntroduce.length != 0) {
                        if (acIcon != null && acIcon.length != 0) {
                            if (acState != null && acState.length != 0) {
                                $.ajax({
                                    type: "POST",
                                    url: "/manage/article/category/update",
                                    data: {
                                        acId: acId,
                                        acTitle: acTitle,
                                        acIntroduce: acIntroduce,
                                        acIcon: acIcon,
                                        acState: acState
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        console.log(data);
                                        if (data.status == 200) {
                                            alert(data.message);
                                            var ac = data.articleCategory;
                                            console.log(ac);
                                            console.log(typeof ac.acState);
                                            $(".acId").val(ac.acId);
                                            $(".acTitle").val(ac.acTitle);
                                            $(".acIntroduce").val(ac.acIntroduce);
                                            $(".acIcon").val(ac.acIcon);
                                            switch (ac.acState) {
                                                case 0: $(".acState").eq(0).attr('checked', true); break;
                                                case 1: $(".acState").eq(1).attr('checked', true); break;
                                                default: alert("设置选中状态bug");
                                            }
                                        }
                                    },
                                    error: function () {
                                        alert("操作失败：Ajax更新错误");
                                    }
                                });
                            } else {
                                alert("操作失败：未设置分类状态");
                            }
                        } else {
                            alert("操作失败：分类图标不能为空");
                        }
                    } else {
                        alert("操作失败：分类介绍不能为空");
                    }
                } else {
                    alert("操作失败：分类标题不能为空");
                }
            } else {
                alert("操作失败：未设置分类状态");
            }
        });
    });
</script>
</html>